package com.quanyan.stadium.main;

import com.alibaba.druid.pool.DruidDataSource;
import com.quanyan.common.interceptor.UserSecurityInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import javax.servlet.Filter;
import javax.sql.DataSource;

/**
 * @author songzj
 * @date 16/4/11-14:09
 */


@Configuration
@ImportResource({"classpath:spring/application-profile.xml"})
@ComponentScan(basePackages = {
        "com.quanyan.redisCluster.config",
        "com.quanyan.common.interceptor",     //参数校验拦截器, RequestBody, ResponseBody拦截. controllerAround拦截
        "com.quanyan.stadium.exception.handler", //全局异常捕获处理
        "com.quanyan.stadium.biz",
        "com.quanyan.stadium.transfer",
        "com.quanyan.stadium.service",
        "com.quanyan.stadium.web",
        "com.quanyan.stadium.component",
        "com.quanyan.stadium.aop"
})
@EnableAutoConfiguration
@MapperScan(basePackages = {"com.quanyan.stadium.mapper"})
@EnableTransactionManagement
@EnableAsync
public class Application {

    /**
     * 启动器
     *
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(Application.class);
        springApplication.run(args);
    }

    @Bean(name = "dataSource", destroyMethod = "close", initMethod = "init")
    @ConfigurationProperties(prefix = "spring.datasource", locations = "classpath:application-alpha.properties")
    public DataSource dataSource() {
        return new DruidDataSource();
    }


    @Bean
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/com/quanyan/stadium/mapper/*.xml"));
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public Filter characterEncodingFilter() {
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceEncoding(true);
        return characterEncodingFilter;
    }


    /**
     * 配置拦截器,在这里配置对controller路径的拦截
     * addInterceptor加入过拦截器/excludePathPatterns从拦截器中排除
     *
     * @author xiaoxiao
     * @param registry
     */
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new UserSecurityInterceptor())
                .addPathPatterns("/api/stadium/gameEvent/**")
                .excludePathPatterns("/api/stadium/gameEvent/gameEventSearchConfig")
                .excludePathPatterns("/api/stadium/gameEvent/gameEventSearch")
                .excludePathPatterns("/api/stadium/gameEvent/gameEventDetail")
                .excludePathPatterns("/api/stadium/gameEvent/gameGroundList")
                .excludePathPatterns("/api/stadium/gameEvent/gameEventData")
                .excludePathPatterns("/api/stadium/gameEvent/gameTeamByGameEventId")
                .addPathPatterns("/api/stadium/gameSchedule/**")
                .addPathPatterns("/api/stadium/pkEvent/**")
                .addPathPatterns("/api/stadium/userCenter/**")
                .addPathPatterns("/api/stadium/gameSignUp/**")
                .addPathPatterns("/api/stadium/radarchart/**")
                .excludePathPatterns("/api/stadium/radarchart/userRadarChart")
                .addPathPatterns("/api/stadium/sportCard/setCategoryPriority")
                .addPathPatterns("/api/stadium/personalGameEvent/createPersonalGame")
                .addPathPatterns("/api/stadium/index/userInfo")
                .excludePathPatterns("/api/stadium/personalGameEvent/selectPersonalGameEventDetail")
                .excludePathPatterns(" /api/stadium/gradeIntegral/getGradeIntegralExplain")
                .addPathPatterns("/api/stadium/gameEvent/createArrangeLiveGameEvent")
                .addPathPatterns("/api/stadium/clubGameEvent/**")
                .excludePathPatterns("/api/stadium/clubGameEvent/cludGameQueryConfig")
                .addPathPatterns("/api/stadium/gradeIntegral/getUserGradeIntegral")
                .addPathPatterns("/api/stadium/gameEventOrder/getGameEventOrderList");
    }
}
